//
//  ExamAnswerSheetView.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/6/5.
//

#import "ExamAnswerSheetView.h"
#import "AnswerSheetCell.h"

@interface ExamAnswerSheetView()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UILabel *countLabel;
@property(nonatomic,strong)UITableView *tableView;

@end

@implementation ExamAnswerSheetView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.currentAvailableWidth = frame.size.width;
        self.currentAvailableHeight = frame.size.height;
        [self createUI];
    }
    return self;
}

-(void)createUI{
   
    self.backgroundColor = UIColorHex_(0x000000,0.4);
    
    CGFloat h582 = [DeviceUtils getAdaptedHeightForDesignHeight:582 andCurrentAvailableHeight:self.currentAvailableHeight];
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0,self.currentAvailableHeight,self.currentAvailableWidth,h582)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.bgView];
    [self.bgView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(10,10)];
    
    UIButton *closeBtn = [[UIButton alloc]init];
    [closeBtn setImage:[UIImage imageNamed:@"answer_sheet_close"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@44);
        make.right.mas_equalTo(@-6);
        make.top.mas_equalTo(@6);
    }];
    UIImageView *logoImageView = [[UIImageView alloc]init];
    logoImageView.image = [UIImage imageNamed:@"exam_question_answer_sheet"];
    [self.bgView addSubview:logoImageView];
    [logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.width.height.mas_equalTo(@20);
        make.centerY.mas_equalTo(closeBtn);
    }];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = JPUIFontPingFangSCMedium(17);
    titleLabel.textColor = KFontBlackColor;
    titleLabel.text = @"答题卡";
    [self.bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@24);
        make.centerY.mas_equalTo(logoImageView);
        make.left.mas_equalTo(logoImageView.mas_right).offset(2);
    }];
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = KLineColor;
    [self.bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(@0);
        make.height.mas_equalTo(@1);
        make.top.mas_equalTo(@52);
    }];
    
    self.countLabel = [[UILabel alloc]init];
    self.countLabel.font = JPUIFontPingFangSCRegular(14);
    self.countLabel.textColor = KFontGrayColor;
    self.countLabel.textAlignment = NSTextAlignmentRight;
    [self.bgView addSubview:self.countLabel];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line.mas_bottom).offset(12);
        make.right.mas_equalTo(@-16);
        make.height.mas_equalTo(@20);
    }];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,94,self.currentAvailableWidth,self.bgView.height-94) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:NSClassFromString(@"AnswerSheetCell") forCellReuseIdentifier:@"AnswerSheetCell"];
    [self.bgView addSubview:self.tableView];
    
}

-(void)refreshWidth{
    CGFloat h582 = [DeviceUtils getAdaptedHeightForDesignHeight:582 andCurrentAvailableHeight:self.currentAvailableHeight];
    if(self.isShow == YES){
        self.bgView.frame = CGRectMake(0,self.currentAvailableHeight-h582,self.currentAvailableWidth,h582);
    }else{
        self.bgView.frame = CGRectMake(0,self.currentAvailableHeight,self.currentAvailableWidth,h582);
    }
    [self.bgView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(10,10)];
    self.tableView.frame = CGRectMake(0,94,self.currentAvailableWidth,self.bgView.height-94);
    [self.tableView reloadData];
}


#pragma mark - tableView
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AnswerSheetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AnswerSheetCell"];
    cell.currentAvailableWidth = self.currentAvailableWidth;
    cell.testType = self.testType;
    if(indexPath.section < self.answerSheetArr.count){
        ExamPaperAnswerSheetModel *model = self.answerSheetArr[indexPath.section];
        if(indexPath.row < model.answers.count){
            ExamPaperAnswerSheetListModel *listmodel = model.answers[indexPath.row];
            cell.answerArr = self.answerArr;
            cell.listModel = listmodel;
            __weak typeof(self)self_weak_ = self;
            [cell setClickBlock:^(ExamPaperAnswerSheetRecordModel * _Nonnull recordModel) {
                if(self_weak_.clickBlock){
                    self_weak_.clickBlock(recordModel);
                }
            }];
        }
    }
   
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ExamPaperAnswerSheetModel *model = self.answerSheetArr[indexPath.section];
    ExamPaperAnswerSheetListModel *listmodel = model.answers[indexPath.row];
    NSInteger count = listmodel.examRecords.count;
    NSInteger rows = (count + 5) / 6;
    return rows * (30 + 22);
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.answerSheetArr.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    ExamPaperAnswerSheetModel *model = self.answerSheetArr[section];
    return model.answers.count;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0,0,self.currentAvailableWidth,43)];
    headView.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(16,0,self.currentAvailableWidth-32,21)];
    titleLabel.font = JPUIFontPingFangSCRegular(15);
    titleLabel.textColor = KFontGrayColor;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    if(section < self.answerSheetArr.count){
        ExamPaperAnswerSheetModel *model = self.answerSheetArr[section];
        titleLabel.text = [NSString stringWithFormat: @"- %@ -",model.typeName];
    }
    [headView addSubview:titleLabel];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 43;
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.currentAvailableWidth,0.00001)];
    footerView.backgroundColor = [UIColor whiteColor];
    return footerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001;
}

#pragma mark - 关闭
-(void)closeBtnClick{
    [self hidden];
}


-(void)show{
    self.isShow = YES;
    CGFloat h582 = [DeviceUtils getAdaptedHeightForDesignHeight:582 andCurrentAvailableHeight:self.currentAvailableHeight];
    self.countLabel.text = [NSString stringWithFormat:@"共%@题",self.count];
    [self.tableView reloadData];
    __weak typeof(self)self_weak_ = self;
    [UIView animateWithDuration:0.2 animations:^{
        self_weak_.bgView.frame = CGRectMake(0,self_weak_.currentAvailableHeight-h582,self_weak_.currentAvailableWidth,h582);
    } completion:^(BOOL finished) {
       
    }];
}

-(void)hidden{
    self.isShow = NO;
    CGFloat h582 = [DeviceUtils getAdaptedHeightForDesignHeight:582 andCurrentAvailableHeight:self.currentAvailableHeight];
    __weak typeof(self)self_weak_ = self;
    [UIView animateWithDuration:0.2 animations:^{
        self_weak_.bgView.frame = CGRectMake(0,self_weak_.currentAvailableHeight,self_weak_.currentAvailableWidth,h582);
    } completion:^(BOOL finished) {
        [self_weak_ removeFromSuperview];
    }];
}

@end
